/**
 * s_acpitemp.c
 * ACPI temperature source for TrayInfo.
 * ACPI temperature support added by Laszlo Ashin <kodest@gmail.com>
 */

#include <stdio.h>
#include <stdlib.h>

#include "trayinfo.h"
#include "utils.h"

#define READPERIOD	10

static int
acpitemp_read(void)
{
	int temp = 0;
	const char *idlist[] = { "temperature" };
	char *vallist[] = { NULL };

	collect_data_from_file(
		"/proc/acpi/thermal_zone/THRM/temperature",
		":", 1, idlist, vallist
	);
	if (*vallist) {
		if (sscanf(*vallist, "%d", &temp) != 1)
			temp = 0;

		free(*vallist);
	}
	return temp;
}

static int
acpitemp_write(struct timeval *now, int fd)
{
	static time_t lasttime = 0;
	static int temp = 0;

	if (now->tv_sec >= lasttime + READPERIOD) {
		temp = acpitemp_read();
		lasttime = now->tv_sec;
	}
	printf("%02dC", temp);
	return 1;
}

void
regsrc_acpitemp(src_t *src)
{
	src->name = "acpitemp";
	src->write = acpitemp_write;
}
